﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;
using System;

public class ScriptHeadWindow : EditorWindow
{
    string _Description = "代码头信息\n在创建脚本文件时下面字段将写入脚步文件内\n下面字段保存在Prefa内，在不删除Prefab前提字段将会保存下去";
    // 是否启用
    static private bool ScriptHeadEnable = false;
    // 代码头信息
    static private string 作者;
    static private string 博客;
    static private string Git;
    static private string 邮箱;
    static private string 描述;

    #region 字段
    const string Author = "XXY";
    const string Blog = "https://www.cnblogs.com/jzyl";
    const string Gitee = "https://gitee.com/xiaobaichong";
    const string Email = "1047185209@QQ.com";
    const string Description = "脚本描述";
    #endregion


    [MenuItem("Tools/ScriptHeadContent")]

    private static void ShowWindow()
    {
        EditorWindow window = EditorWindow.GetWindow(typeof(ScriptHeadWindow));
        window.titleContent = new GUIContent("代码头信息");

        作者 = EditorPrefs.GetString("作者", ScriptHeadWindow.Author);
        博客 = EditorPrefs.GetString("博客", ScriptHeadWindow.Blog);
        Git = EditorPrefs.GetString("Git", ScriptHeadWindow.Gitee);
        邮箱 = EditorPrefs.GetString("邮箱", ScriptHeadWindow.Email);
        描述 = EditorPrefs.GetString("描述", ScriptHeadWindow.Description);
        ScriptHeadEnable = EditorPrefs.GetInt("ScriptHeadEnable", 0) == 1;
    }
    private void OnGUI()
    {
        GUILayout.Label(_Description);
        ScriptHeadEnable = EditorGUILayout.Toggle("是否启用代码信息注入", ScriptHeadEnable);
        作者 = EditorGUILayout.TextField("作者", 作者);
        博客 = EditorGUILayout.TextField("博客", 博客);
        Git = EditorGUILayout.TextField("Git", Git);
        邮箱 = EditorGUILayout.TextField("邮箱", 邮箱);
        描述 = EditorGUILayout.TextField("描述", 描述);
        if (GUILayout.Button("保存"))
        {
            EditorPrefs.SetString("作者", 作者);
            EditorPrefs.SetString("博客", 博客);
            EditorPrefs.SetString("Git", Git);
            EditorPrefs.SetString("邮箱", 邮箱);
            EditorPrefs.SetString("描述", 描述);
            EditorPrefs.SetInt("ScriptHeadEnable", ScriptHeadEnable ? 1 : 0);
            Debug.Log("<color=yellow>保存成功</color>");
        }
    }


    public class ScriptHeadComment : UnityEditor.AssetModificationProcessor
    {
        /// <summary>
        /// Unity 在即将创建未导入的资源（例如，.meta 文件）时调用此方法。
        /// </summary>
        /// <param name="filePath"></param>
        private static void OnWillCreateAsset(string filePath)
        {
            // 是C# 脚本文件
            // ScriptHeadEnable 已启用
            if (filePath.EndsWith(".cs.meta"))
            {
                filePath = filePath.Substring(0, filePath.Length - 5);
                WriteHeadComment(filePath);
            }
        }
        private static void WriteHeadComment(string filePath)
        {
            /************************************************
              * Author       :   xxy
              * Blog         :   https://www.cnblogs.com/jzyl
              * Gitee        :   https://gitee.com/xiaobaichong
              * Email        :   1047185209@QQ.com
              * FileName     :   ******.cs
              * CreateData   :   2020/3/3 20:30
              * UnityVersion :   2020.3.33
              * Description  :   描述
              ************************************************/
            string allText = File.ReadAllText(filePath);
            string title = "" +
                "/************************************************" + "\n" +
                " * Author       :   " + EditorPrefs.GetString("作者", ScriptHeadWindow.Author) + "\n" +
                " * Blog         :   " + EditorPrefs.GetString("博客", ScriptHeadWindow.Blog) + "\n" +
                " * Gitee        :   " + EditorPrefs.GetString("Git", ScriptHeadWindow.Gitee) + "\n" +
                " * Email        :   " + EditorPrefs.GetString("邮箱", ScriptHeadWindow.Email) + "\n" +
                " * FileName     :   " + Path.GetFileName(filePath) + "\n" +
                " * CreateData   :   " + DateTime.Now.ToString() + "\n" +
                " * UnityVersion :   " + Application.unityVersion + "\n" +
                " * Description  :   " + EditorPrefs.GetString("描述", ScriptHeadWindow.Description) + "\n" +
                "************************************************/" + "\n\n";
            File.WriteAllText(filePath, title + allText, System.Text.Encoding.UTF8);
        }
    }
}